import { createSlice } from "@reduxjs/toolkit"



export const waitIncrementAction = (payload) => {
    return (dispatch, getState) => {
        setTimeout(() => {
            dispatch(waitIncrement(payload))
        }, 2000)
    }
}

const countSlice = createSlice({
    name: 'counter',
    initialState: {
        count: 0
    },
    reducers: {
        increment(prev, { payload }) {
            prev.count += payload
        },
        decrement(prev, { payload }) {
            prev.count -= payload
        },
        ifOddIncrement(prev, { payload }) {
            if (prev.count % 2 !== 0) {
                prev.count += payload
            }
        }, 
        waitIncrement(prev, { payload }){
            prev.count += payload
        }
    }
})

export default countSlice.reducer

export const {
    increment,
    decrement,
    ifOddIncrement,
    waitIncrement
} = countSlice.actions

